#[cfg(test)]
mod sql_editor_features_tests {
    use cdb_gui::components::sql_editor::{SqlEditor, SqlHighlighter};
    use cdb_gui::components::database_explorer::{DatabaseExplorer, DatabaseObject, DatabaseObjectType};
    use cdb_gui::components::query_history::{QueryHistory, QueryHistoryItem, QueryStatus};
    use cdb_gui::theme::CdbTheme;

    #[test]
    fn test_sql_highlighter_keywords() {
        let highlighter = SqlHighlighter::new();
        
        // 测试SQL关键字识别
        assert!(highlighter.is_keyword("SELECT"));
        assert!(highlighter.is_keyword("select"));
        assert!(highlighter.is_keyword("FROM"));
        assert!(highlighter.is_keyword("WHERE"));
        assert!(highlighter.is_keyword("INSERT"));
        assert!(highlighter.is_keyword("UPDATE"));
        assert!(highlighter.is_keyword("DELETE"));
        
        // 测试非关键字
        assert!(!highlighter.is_keyword("table_name"));
        assert!(!highlighter.is_keyword("column_name"));
        assert!(!highlighter.is_keyword("user_data"));
    }

    #[test]
    fn test_sql_highlighter_functions() {
        let highlighter = SqlHighlighter::new();
        
        // 测试SQL函数识别
        assert!(highlighter.is_function("COUNT"));
        assert!(highlighter.is_function("count"));
        assert!(highlighter.is_function("SUM"));
        assert!(highlighter.is_function("AVG"));
        assert!(highlighter.is_function("MAX"));
        assert!(highlighter.is_function("MIN"));
        assert!(highlighter.is_function("UPPER"));
        assert!(highlighter.is_function("LOWER"));
        
        // 测试非函数
        assert!(!highlighter.is_function("my_function"));
        assert!(!highlighter.is_function("custom_proc"));
    }

    #[test]
    fn test_sql_highlighter_operators() {
        let highlighter = SqlHighlighter::new();
        
        // 测试操作符识别
        assert!(highlighter.is_operator("="));
        assert!(highlighter.is_operator("!="));
        assert!(highlighter.is_operator("<>"));
        assert!(highlighter.is_operator("<"));
        assert!(highlighter.is_operator(">"));
        assert!(highlighter.is_operator("<="));
        assert!(highlighter.is_operator(">="));
        
        // 测试非操作符
        assert!(!highlighter.is_operator("AND"));
        assert!(!highlighter.is_operator("OR"));
    }

    #[test]
    fn test_sql_editor_creation() {
        let editor = SqlEditor::new();
        
        assert_eq!(editor.text(), "");
        assert!(editor.content().text().is_empty());
    }

    #[test]
    fn test_sql_editor_with_content() {
        let sql = "SELECT * FROM users WHERE age > 18";
        let editor = SqlEditor::new().with_content(sql);
        
        assert_eq!(editor.text(), sql);
    }

    #[test]
    fn test_sql_editor_line_numbers() {
        let editor = SqlEditor::new().with_line_numbers(true);
        // 行号功能已启用，这里主要测试配置是否正确设置
        
        let editor_no_lines = SqlEditor::new().with_line_numbers(false);
        // 测试禁用行号的情况
    }

    #[test]
    fn test_sql_editor_auto_indent() {
        let editor = SqlEditor::new().with_auto_indent(true);
        // 自动缩进功能已启用
        
        let editor_no_indent = SqlEditor::new().with_auto_indent(false);
        // 测试禁用自动缩进的情况
    }

    #[test]
    fn test_database_object_creation() {
        let obj = DatabaseObject::new(
            "table_1".to_string(),
            "users".to_string(),
            DatabaseObjectType::Table,
        );
        
        assert_eq!(obj.id, "table_1");
        assert_eq!(obj.name, "users");
        assert_eq!(obj.object_type, DatabaseObjectType::Table);
        assert!(!obj.is_expanded);
        assert!(!obj.is_favorite);
        assert!(obj.children.is_empty());
    }

    #[test]
    fn test_database_object_type_icons() {
        assert_eq!(DatabaseObjectType::Database.icon(), "🗄️");
        assert_eq!(DatabaseObjectType::Table.icon(), "📊");
        assert_eq!(DatabaseObjectType::View.icon(), "👁️");
        assert_eq!(DatabaseObjectType::Procedure.icon(), "⚙️");
        assert_eq!(DatabaseObjectType::Function.icon(), "🔧");
        assert_eq!(DatabaseObjectType::Trigger.icon(), "⚡");
        assert_eq!(DatabaseObjectType::Index.icon(), "📇");
        assert_eq!(DatabaseObjectType::Schema.icon(), "📁");
        assert_eq!(DatabaseObjectType::Connection.icon(), "🖥️");
    }

    #[test]
    fn test_database_object_type_display_names() {
        assert_eq!(DatabaseObjectType::Database.display_name(), "数据库");
        assert_eq!(DatabaseObjectType::Table.display_name(), "表");
        assert_eq!(DatabaseObjectType::View.display_name(), "视图");
        assert_eq!(DatabaseObjectType::Procedure.display_name(), "存储过程");
        assert_eq!(DatabaseObjectType::Function.display_name(), "函数");
    }

    #[test]
    fn test_database_object_search() {
        let obj = DatabaseObject::new(
            "table_1".to_string(),
            "user_accounts".to_string(),
            DatabaseObjectType::Table,
        );
        
        // 测试搜索匹配
        assert!(obj.matches_search("user"));
        assert!(obj.matches_search("USER"));
        assert!(obj.matches_search("account"));
        assert!(obj.matches_search("表"));
        assert!(obj.matches_search(""));
        
        // 测试搜索不匹配
        assert!(!obj.matches_search("order"));
        assert!(!obj.matches_search("product"));
    }

    #[test]
    fn test_database_object_type_filter() {
        let table_obj = DatabaseObject::new(
            "table_1".to_string(),
            "users".to_string(),
            DatabaseObjectType::Table,
        );
        
        let view_obj = DatabaseObject::new(
            "view_1".to_string(),
            "user_view".to_string(),
            DatabaseObjectType::View,
        );
        
        // 测试类型过滤
        assert!(table_obj.matches_type_filter(&Some(DatabaseObjectType::Table)));
        assert!(!table_obj.matches_type_filter(&Some(DatabaseObjectType::View)));
        assert!(table_obj.matches_type_filter(&None));
        
        assert!(view_obj.matches_type_filter(&Some(DatabaseObjectType::View)));
        assert!(!view_obj.matches_type_filter(&Some(DatabaseObjectType::Table)));
        assert!(view_obj.matches_type_filter(&None));
    }

    #[test]
    fn test_database_explorer_creation() {
        let explorer = DatabaseExplorer::new();
        
        // 验证示例数据已加载
        // 这里主要测试创建过程没有错误
    }

    #[test]
    fn test_query_history_creation() {
        let history = QueryHistory::new();
        
        // 验证初始状态
        // 历史记录应该为空
    }

    #[test]
    fn test_query_history_add_query() {
        let mut history = QueryHistory::new();
        
        let query = "SELECT * FROM users".to_string();
        let database = "test_db".to_string();
        
        let id = history.add_query(query.clone(), database.clone());
        
        // 验证查询已添加
        assert!(!id.is_empty());
    }

    #[test]
    fn test_query_history_item_creation() {
        let query = "SELECT COUNT(*) FROM orders".to_string();
        let database = "ecommerce".to_string();
        
        let item = QueryHistoryItem::new(query.clone(), database.clone());
        
        assert_eq!(item.query, query);
        assert_eq!(item.database, database);
        assert_eq!(item.status, QueryStatus::Running);
        assert!(!item.is_favorite);
        assert!(item.execution_time.is_none());
        assert!(item.result_count.is_none());
    }

    #[test]
    fn test_query_status_icons() {
        assert_eq!(QueryStatus::Success.icon(), "✅");
        assert_eq!(QueryStatus::Error("test".to_string()).icon(), "❌");
        assert_eq!(QueryStatus::Running.icon(), "⏳");
        assert_eq!(QueryStatus::Cancelled.icon(), "⏹️");
    }

    #[test]
    fn test_query_history_item_formatting() {
        let mut item = QueryHistoryItem::new(
            "SELECT id, name, email FROM users WHERE created_at > '2023-01-01' ORDER BY name".to_string(),
            "main_db".to_string(),
        );
        
        item.execution_time = Some(1500); // 1.5秒
        
        // 测试查询预览（应该被截断）
        let preview = item.query_preview();
        assert!(preview.len() <= 50);
        assert!(preview.contains("SELECT"));
        
        // 测试执行时间格式化
        assert_eq!(item.formatted_execution_time(), "1.5s");
        
        // 测试短执行时间
        item.execution_time = Some(250);
        assert_eq!(item.formatted_execution_time(), "250ms");
        
        // 测试无执行时间
        item.execution_time = None;
        assert_eq!(item.formatted_execution_time(), "N/A");
    }

    #[test]
    fn test_query_history_update_status() {
        let mut history = QueryHistory::new();
        
        let id = history.add_query("SELECT 1".to_string(), "test".to_string());
        
        // 更新状态为成功
        history.update_query_status(
            &id,
            QueryStatus::Success,
            Some(100),
            Some(1),
        );
        
        // 验证状态已更新（这里主要测试方法调用不出错）
    }

    #[test]
    fn test_query_history_toggle_favorite() {
        let mut history = QueryHistory::new();
        
        let id = history.add_query("SELECT * FROM favorites".to_string(), "test".to_string());
        
        // 切换收藏状态
        history.toggle_favorite(&id);
        
        // 再次切换
        history.toggle_favorite(&id);
        
        // 验证操作完成（主要测试方法调用不出错）
    }

    #[test]
    fn test_query_history_delete_query() {
        let mut history = QueryHistory::new();
        
        let id1 = history.add_query("SELECT 1".to_string(), "test".to_string());
        let id2 = history.add_query("SELECT 2".to_string(), "test".to_string());
        
        // 删除第一个查询
        history.delete_query(&id1);
        
        // 验证删除操作（主要测试方法调用不出错）
    }

    #[test]
    fn test_query_history_clear() {
        let mut history = QueryHistory::new();
        
        history.add_query("SELECT 1".to_string(), "test".to_string());
        history.add_query("SELECT 2".to_string(), "test".to_string());
        
        // 清空历史记录
        history.clear_history();
        
        // 验证清空操作（主要测试方法调用不出错）
    }

    #[test]
    fn test_sql_formatting() {
        let editor = SqlEditor::new();
        
        // 测试简单SQL格式化
        let simple_sql = "select id,name from users where age>18";
        let formatted = editor.format_sql_text(simple_sql);
        
        // 验证格式化结果包含适当的缩进和换行
        assert!(formatted.contains("SELECT") || formatted.contains("select"));
        assert!(formatted.contains("FROM") || formatted.contains("from"));
        assert!(formatted.contains("WHERE") || formatted.contains("where"));
    }

    #[test]
    fn test_integration_sql_editor_and_history() {
        let mut editor = SqlEditor::new();
        let mut history = QueryHistory::new();
        
        // 模拟用户输入SQL并执行
        let sql = "SELECT COUNT(*) FROM users WHERE active = 1";
        editor = editor.with_content(sql);
        
        // 添加到历史记录
        let id = history.add_query(editor.text(), "production".to_string());
        
        // 模拟执行完成
        history.update_query_status(&id, QueryStatus::Success, Some(150), Some(1));
        
        // 验证集成操作完成
        assert_eq!(editor.text(), sql);
    }
}
